package view;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;

import net.miginfocom.swing.MigLayout;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.Vector;


public class HomePage extends JPanel{

	//Background image
	private BufferedImage image;
	//To switch panels
	private WorkSpacePanel workspacePanel;
	
	//Buttons
	private JButton Employee;
	private JButton Message;
	private JButton Help;
	private JButton Budget;
	private JButton Floors;
	
	
	public HomePage(WorkSpacePanel wsp)
	{
		this.setLayout(null);
		workspacePanel = wsp;
		init();
	}
	
	public void init()
	{		
		
		// Read image.
        try 
        {
            image = ImageIO.read(new File(
                    "images/homepagebg.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        Image img;
        Image img2;
                
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
          } catch(Exception e) {
            System.out.println("Error setting native LAF: " + e);
          }

        //Set our button icons
        try {
			img = ImageIO.read(new File("images/homepageEmployee2.jpg"));
			img2 = ImageIO.read(new File("images/homepageEmployee.jpg"));
			Employee = new JButton(new ImageIcon(img.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Employee.setPressedIcon(new ImageIcon(img2.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Employee.setBounds(100, 200, 300, 120);
			Employee.setOpaque(false); 
			Employee.setContentAreaFilled(false);
			Employee.addActionListener(new EmployeeButtonListener());
			Employee.setBorderPainted(false);
			img = ImageIO.read(new File("images/homepageBudget.jpg"));
			img2 = ImageIO.read(new File("images/homepageBudget2.jpg"));									
			Budget = new JButton(new ImageIcon(img.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Budget.setPressedIcon(new ImageIcon(img2.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Budget.setBounds(100, 320, 300, 120);
			Budget.setOpaque(false); 
			Budget.setContentAreaFilled(false); 
			Budget.setBorderPainted(false);
			Budget.addActionListener(new BudgetButtonListener());
			img = ImageIO.read(new File("images/homepageFloors.jpg"));
			img2 = ImageIO.read(new File("images/homepageFloors2.jpg"));									
			Floors = new JButton(new ImageIcon(img.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Floors.setPressedIcon(new ImageIcon(img2.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Floors.setBounds(100, 440, 300, 120);
			Floors.setOpaque(false); 
			Floors.setContentAreaFilled(false); 
			Floors.setBorderPainted(false);
			Floors.addActionListener(new FloorsButtonListener());
			img = ImageIO.read(new File("images/homepageMessages.jpg"));
			img2 = ImageIO.read(new File("images/homepageMessages2.jpg"));									
			Message = new JButton(new ImageIcon(img.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Message.setPressedIcon(new ImageIcon(img2.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Message.setBounds(100, 560, 300, 120);
			Message.setOpaque(false); 
			Message.setContentAreaFilled(false); 
			Message.setBorderPainted(false);
			Message.addActionListener(new MessagesButtonListener());
			img = ImageIO.read(new File("images/homepageHelp.jpg"));
			img2 = ImageIO.read(new File("images/homepageHelp2.jpg"));									
			Help = new JButton(new ImageIcon(img.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Help.setPressedIcon(new ImageIcon(img2.getScaledInstance( 300,120,  java.awt.Image.SCALE_SMOOTH )));
			Help.setBounds(100, 680, 300, 120);
			Help.setOpaque(false); 
			Help.setContentAreaFilled(false); 
			Help.setBorderPainted(false);
			Help.addActionListener(new HelpButtonListener());
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        //Add buttons to Panel
        this.add(Employee);
        this.add(Budget);
        this.add(Floors);
        this.add(Message);
        this.add(Help);
	}
	/**
	 * Make image background transparent 
	 * @throws IOException 
	 */
	public Image transparent(Image img) throws IOException
	{		
		BufferedImage imageToLoad = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		Graphics g = imageToLoad.getGraphics();
		g.drawImage(img, 0,	0,	null);
		
		WritableRaster raster = imageToLoad.getRaster();
		int[] dummy = null;
		for (int i = 0; i < raster.getWidth(); i++)
		{
			for (int j = 0; j < raster.getHeight(); j++)
			{
				int[] pixel = raster.getPixel(i, j, dummy);
				if ((pixel[0] > 200) && (pixel[1] > 200) && (pixel[2] > 200))
				{
						pixel[3] = 0;
						raster.setPixel(i, j, pixel);					
				}
			}
		}		
		return imageToLoad;
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Font f = new Font("Calibri", Font.PLAIN, 50);
		Dimension size = this.getSize();		
		// RENDER THE BACKGROUND IMAGE
		this.setFont(f);
		g.drawImage(image, 0, 0,size.width,size.height, null);
		g.setColor(Color.white);
		g.drawString(" Welcome to the Hotel Management System ", 50, 100);		
	}
	/**
	 * 
	 * @author shin
	 *
	 */
	class EmployeeButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			workspacePanel.setPanel("employeesPanel");
		}
	}
	/**
	 * 
	 * @author shin
	 *
	 */
	class BudgetButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			workspacePanel.setPanel("budgetPanel");
		}
	}
	/**
	 * 
	 * @author shin
	 *
	 */
	class FloorsButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			workspacePanel.setPanel("floorPanel");
		}
	}
	/**
	 * 
	 * @author shin
	 *
	 */
	class HelpButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			workspacePanel.setPanel("helpPanel");
		}
	}
	/**
	 * 
	 * @author shin
	 *
	 */
	class MessagesButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			workspacePanel.setPanel("messagePanel");
		}
	}

}
